package com.jyh.service.loan;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.jyh.config.BaseAction;
import com.jyh.config.datasource.TargetDataSource;
import com.jyh.db.entity.JyhConfig;
import com.jyh.db.entity.JyhConfigExample;
import com.jyh.db.mapper.JyhConfigMapper;
import com.jyh.dbOrder.dao.LoanDao;
import com.jyh.service.ButtonService;
import com.jyh.util.StringUtils;

@Service
public class LoanTrackService extends BaseAction {
	@Autowired
	private LoanDao loanDao;
	@Autowired
	private JyhConfigMapper configMapper;
	@Autowired
	private ButtonService buttonService;
	/**
	 * 获取贷款跟踪页面数据
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getLoanTrack(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("loan/loanTrack");
		String userNO = request.getParameter("userNO");
		String orderNO = request.getParameter("orderNO");
		String userName = request.getParameter("userName");
		String score1 = request.getParameter("score1");
		String score2 = request.getParameter("score2");
		String orderTime1 = request.getParameter("orderTime1");
		String orderTime2 = request.getParameter("orderTime2");
		String state = request.getParameter("state");
		String page = request.getParameter("currentPage");// 传过来的页数
		int lineSize = 20; // 每页显示记录20条数
		int pageCount = 0; // 总页数
		int count = 0; // 数据总数
		int currentPage = 1; // 当前是第几页,默认是第一页
		if (page == null || "1".equals(page)) {
		} else {
			currentPage = Integer.parseInt(page); // 当前是第几页,默认是第一页
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		String limitSql = " limit " + (currentPage - 1) * lineSize + "," + lineSize;

		String sql = "select t1.orderTime,t1.orderNO,t1.userNO,t4.userName,t3.money,t3.dayRates,t3.loanTerm,t3.rateSum,"
				+ " t3.serviceMoney,t5.valName,t1.score from jyh_order t1 left join jyh_loan_type t3 on t1.typeNO=t3.typeNO "
				+ " left join jyh_user_msg t4 on t1.orderNO=t4.orderNo "
				+ " left join (SELECT * FROM jyh_config WHERE groups='state') t5 on t5.value=t1.state where 1=1";
		String sql1 = "select count(1) as count from jyh_order t1 left join jyh_loan_type t3 on t1.typeNO=t3.typeNO "
				+ " left join jyh_user_msg t4 on t1.orderNO=t4.orderNo "
				+ " left join (SELECT * FROM jyh_config WHERE groups='state') t5 on t5.value=t1.state where 1=1";
		if (StringUtils.isNotNull(userNO)) {
			sql = sql + " and t1.userNO='" + userNO + "'";
			sql1 = sql1 + " and t1.userNO='" + userNO + "'";
		}
		if (StringUtils.isNotNull(orderNO)) {
			sql = sql + " and t1.orderNO='" + orderNO + "'";
			sql1 = sql1 + " and t1.orderNO='" + orderNO + "'";
		}
		if (StringUtils.isNotNull(userName)) {
			sql = sql + " and t4.userName<='" + userName + "'";
			sql1 = sql1 + " and t4.userName<='" + userName + "'";
		}
		if (StringUtils.isNotNull(score1)) {
			sql = sql + " and t1.score>='" + score1 + "'";
			sql1 = sql1 + " and t1.score>='" + score1 + "'";
		}
		if (StringUtils.isNotNull(score2)) {
			sql = sql + " and t1.score<='" + score2 + "'";
			sql1 = sql1 + " and t1.score<='" + score2 + "'";
		}
		if (StringUtils.isNotNull(orderTime1)) {
			sql = sql + " and t1.orderTime>='" + orderTime1 + "'";
			sql1 = sql1 + " and t1.orderTime>='" + orderTime1 + "'";
		}
		if (StringUtils.isNotNull(orderTime2)) {
			sql = sql + " and t1.orderTime<='" + orderTime2 + "'";
			sql1 = sql1 + " and t1.orderTime<='" + orderTime2 + "'";
		}
		if (StringUtils.isNotNull(state)) {
			sql = sql + " and t1.state='" + state + "'";
			sql1 = sql1 + " and t1.state='" + state + "'";
		}
		
		Map<String, Object> map = loanDao.getMap(sql1);
		count = Integer.parseInt(map.get("count") + "");
		if(count<=lineSize){
			limitSql = " limit 0," + lineSize;
		}
		sql = sql + " order by t1.orderTime desc"+ limitSql;
		List<Map<String, Object>> list = loanDao.getList(sql);
		
		int temp = count / lineSize; // 取商
		int tem2 = count % lineSize;// 取余数
		if (tem2 > 0 && tem2 < lineSize && temp > 0) {
			pageCount = count / lineSize + 1; // 总页数
		} else if (tem2 == 0 && temp > 0) {
			pageCount = temp;
		} else {
			pageCount = 1; // 总页数
		}
		if (currentPage >= pageCount) {
			currentPage = pageCount;
		}

		// 获取状态
		JyhConfigExample example = new JyhConfigExample();
		example.createCriteria().andGroupsEqualTo("state");
		List<JyhConfig> stateList = configMapper.selectByExample(example);
//		logger.info("获取状态stateList={}", JSONObject.toJSONString(stateList));
		list = (List<Map<String, Object>>) buttonService.getTableButton(14, "orderNO", jsonOutSimplify(list), session);
		mv.addObject("pageCount", pageCount);
		mv.addObject("currentPage", currentPage);
		mv.addObject("count", count);
		mv.addObject("stateList", stateList);
		mv.addObject("list", list);
		//将传递过来的参数回显
		mv.addObject("orderNO", orderNO);
		mv.addObject("userName", userName);
		mv.addObject("userNO", userNO);
		mv.addObject("state", state);
		mv.addObject("score1", score1);
		mv.addObject("score2", score2);
		mv.addObject("orderTime1", orderTime1);
		mv.addObject("orderTime2", orderTime2);
		
		return mv;
	}

}
